﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HZ.Public;

namespace WpfAppDemo
{
    /// <summary>
    /// UserControl2.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl2 : UserControl
    {
        public UserControl2()
        {
            InitializeComponent();
            ChangeLanguage();
            if (CurrentData == null)
            {
                CurrentData = new RuleProtoclClass();
            }
            this.DataContext = CurrentData;
        }

        public void ChangeLanguage()
        {
            btnOK.Content = PublicClass.GetLanguage("确认");
            btnCancel.Content = PublicClass.GetLanguage("取消");
            txtRuleType.Text = PublicClass.GetLanguage("策略类型");
            txtExecRate.Text = PublicClass.GetLanguage("执行频率");
            txtExecStep.Text = PublicClass.GetLanguage("执行间隔");
            txtExecTime.Text = PublicClass.GetLanguage("执行时间");
            txtExecType.Text = PublicClass.GetLanguage("执行操作");
            txtDesc.Text = PublicClass.GetLanguage("整点");
            (cmbType.Items[0] as ComboBoxItem).Content = PublicClass.GetLanguage("天");
            (cmbType.Items[1] as ComboBoxItem).Content = PublicClass.GetLanguage("周");
            (cmbType.Items[2] as ComboBoxItem).Content = PublicClass.GetLanguage("月");
            (cmbType.Items[3] as ComboBoxItem).Content = PublicClass.GetLanguage("年");
            BtnTime.Content = PublicClass.GetLanguage("按时间");
            btnDelete.Content = PublicClass.GetLanguage("删除");
            btnBackup.Content = PublicClass.GetLanguage("备份");
        }

        public UserControl2(RuleProtoclClass param, int index)
        {
            InitializeComponent();
            if (param == null)
            {
                CurrentData = new RuleProtoclClass();
            }
            else
            {
                CurrentData = param;
            }
            this.DataContext = CurrentData;
            this.Index = index;
            EditMode = 1;
        }

        public int EditMode { get; set; } = 0;

        public int Index = 0;

        public RuleProtoclClass CurrentData { get; set; } = null;

        void CloseWin()
        {
            Window win = PublicClass.FindVisualParent<Window>(this);
            if (win != null)
            {
                win.Close();
            }
        }

        private string GetRuleDesc()
        {
            string desc = "";
            string act = "";
            if (CurrentData.ActType == 0)
            {
                act = PublicClass.GetLanguage("自动删除");
            }
            else
            {
                act = PublicClass.GetLanguage("自动备份");
            }
            desc = string.Format(PublicClass.GetLanguage("系统将每隔{0}{1}对数据进行{2}"), CurrentData.Step, cmbType.Text, act);


            return desc;
        }


        private void BtnOK_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentData.Type == 0 && (CurrentData.Step < 1 || CurrentData.Step > 31))
            {
                PublicClass.ShowMsgBox(PublicClass.GetLanguage("请输入正确的时间间隔！"), false);
                return;
            }

            CurrentData.RuleDesc = GetRuleDesc();
            CurrentData.NextExecTime = CurrentData.GetNextExecTime(CurrentData.NextExecTime, CurrentData);
            if (EditMode == 0)
            {
                RuleData.RuleList.Add(CurrentData);
            }
            else
            {
                RuleData.RuleList[Index] = CurrentData;
            }
            CloseWin();
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            CloseWin();
        }

    }
}